/**
 * @description 地址数据操作
 * @author liyunfu
 */

const Address = require('../../models/Address')

!(async () => {
  //创建收货地址
  // await Address.create({
  //   username:'张三',
  //   city:'北京市',
  //   department:'xxx小区',
  //   houseNumber:'门牌号1',
  //   name:'张三',
  //   phone:'12345678912'
  // })

  // await Address.create({
  //   username:'张三',
  //   city:'北京市',
  //   department:'yyy小区',
  //   houseNumber:'门牌号2',
  //   name:'张三',
  //   phone:'12345678922'
  // })

  //获取收货地址列表
  // const addressList = await Address.find({ username: '张三' }).sort({updatedAt:-1})
  // console.log(addressList);

  //删除收货地址
  // await Address.deleteMany({username:'张三'})

  //根据id获取单个收货地址
  // const id='61d0fcdd33b11f03c784e61a'
  // const address=await Address.findById(id)
  // console.log(address)

  //更新收货地址
  const id = '61d0fcdd33b11f03c784e61a'
  const newData = {
    username: '张三',
    city: '北京市',
    department: 'yyy小区a',
    houseNumber: '门牌号2a',
    name: '张三1',
    phone: '12345678932'
  }
  const address = await Address.findOneAndUpdate({
    _id: id, username: '张三'
  }, newData, {
    new: true  //返回更新之后的最新数据 默认是false 返回更新之前的数据
  })
  console.log(address);
})()
